package com.dycx.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.system.dto.RoleDTO;
import com.dycx.system.entity.RoleDO;
import com.dycx.system.mapper.RoleMapper;
import com.dycx.system.service.PermissionService;
import com.dycx.system.service.RolePermissionService;
import com.dycx.system.service.RoleService;
import com.dycx.system.vo.PermissionVO;
import com.dycx.system.vo.RoleVO;
import com.dycx.system.vo.SystemConfig;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class RoleImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleService {

    private RolePermissionService rolePermissionService;
    private PermissionService permissionService;

    /**
     * 获取所有权限id
     *
     * @return
     */
    @Override
    public List<String> getAllRolePermissionIds() {
        return this.getAllRoleActionArrDg(permissionService.getAllList());
    }

    private List<String> getAllRoleActionArrDg(List<PermissionVO> list) {
        List<String> permissionIds = new ArrayList<>();
        list.forEach(item -> {
            permissionIds.add(item.getPermissionId());
            if (item.getChildren() != null && item.getChildren().size() > 0) {
                permissionIds.addAll(this.getAllRoleActionArrDg(item.getChildren()));
            }
        });

        return permissionIds;
    }

    @Override
    public Boolean delete(int id) {
        rolePermissionService.delete(id);

        return this.removeById(id);
    }

    /**
     * 添加
     *
     * @param roleDto
     * @return
     */
    @Override
    public Boolean insert(RoleDTO roleDto) {
        RoleDO role = BeanUtil.toBean(roleDto, RoleDO.class);
        roleDto.setId(role.getId());
        return this.save(role);
    }

    /**
     * 添加/修改
     *
     * @param roleDto
     * @return
     */
    @Override
    public Boolean update(RoleDTO roleDto) {
        RoleDO role = BeanUtil.toBean(roleDto, RoleDO.class);
        return this.updateById(role);
    }

    /**
     * 获取父角色下的所有角色
     *
     * @param parentId
     * @return
     */
    @Override
    public List<RoleVO> getAllList(Integer parentId) {
        LambdaQueryWrapper<RoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(RoleDO::getId);
        queryWrapper.eq(RoleDO::getParentId, parentId);

        List<RoleDO> roleList = this.list(queryWrapper);

        List<RoleVO> roleVOList = roleList.stream().map(item ->
                BeanUtil.toBean(item, RoleVO.class)
        ).collect(Collectors.toList());

        for (RoleVO roleVo : roleVOList) {
            roleVo.setChildren(getAllList(roleVo.getId()));
        }

        return roleVOList;
    }

    /**
     * 根据路由地址获取权限id
     *
     * @param router 路由地址
     * @return
     */
    public List<String> getActionIdByRouter(List<PermissionVO> list, String router) {
        if (list == null || list.size() == 0) return null;
        List<String> permissionIds = new ArrayList<>();

        for (PermissionVO item : list) {
            List<PermissionVO> itemChildren = item.getChildren();

            if (itemChildren != null && itemChildren.size() > 0) {
                List<String> childrenActionIds = getActionIdByRouter(itemChildren, router);
                if (ObjectUtil.isNotEmpty(childrenActionIds)) {
                    permissionIds.addAll(childrenActionIds);
                }
            } else {
                if (item.getRouter() != null && item.getRouter().size() > 0 && item.getRouter().contains(router)) {
                    permissionIds.add(item.getPermissionId());
                }
            }
        }

        return permissionIds;
    }
}
